package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static org.springframework.beans.BeanUtils.copyProperties;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 保存类别信息
     *
     * 该方法接收一个类别数据传输对象（CategoryDTO），将其属性复制到一个新的类别实体（Category）中，
     * 设置必要的状态和时间字段，以及创建和更新用户信息，最后调用类别映射器（categoryMapper）的插入方法
     * 将新的类别实体保存到数据库中
     *
     * @param categoryDTO 类别数据传输对象，包含要保存的类别的信息
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        // 创建一个新的类别实体对象
        Category category = new Category();

        // 将类别DTO的属性复制到类别实体对象中
        copyProperties(categoryDTO, category);

        // 设置类别状态为启用
        category.setStatus(StatusConstant.ENABLE);

        // 调用类别映射器的插入方法，将新的类别实体保存到数据库中
        categoryMapper.insert(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        List<Category> rows = categoryMapper.pageQuery(categoryPageQueryDTO);
        Page<Category> p = (Page<Category>) rows;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public List<Category> getByType(Integer type) {
        List<Category> categories = categoryMapper.getByType(type);
        return categories;
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    @Override
    public void delete(Long id) {

        Integer status = categoryMapper.getById(id).getStatus();
        if (Objects.equals(status, StatusConstant.ENABLE)) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }

        Long categoryId = id;
        Integer dishCount = dishMapper.countByCategoryId(categoryId);
        if (dishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        Integer setmealCount = setmealMapper.countByCategoryId(categoryId);
        if (setmealCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }


        categoryMapper.delete(id);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.update(category);
    }

}
